<template>
  <el-dialog title="新增档案" :visible.sync="dialogVisible" width="400px" append-to-body>
    <el-form ref="form" :model="form" label-width="100px" :rules="formRules">
      <el-form-item label="所属人员" prop="personId">
        <person-select v-model="form.personId"/>
        <person-select disabled v-model="form.personId"/>
      </el-form-item>
      <el-form-item label="档案类型" prop="fileType">
        <tree-select v-model="form.fileType"
                     :disabled="true"
                     @select="typeSelect"
                     :options="directoryTree"
                     disable-branch-nodes
                     placeholder="选择档案类型"/>
      </el-form-item>
      <el-form-item label="档案名称" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="形成时间" prop="fileFromTime">
        <el-date-picker
            v-model="form.fileFromTime"
            value-format="yyyy-MM-dd"
            placeholder="选择形成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="档案封面" prop="coverImage">
        <image-upload v-model="form.coverImage" :limit="1" :is-show-tip="false"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import TreeSelect from "@riophae/vue-treeselect";
import PersonSelect from "@/components/PersonSelect/index.vue";
import {directoryTree} from "@/api/business/directory";
import {addPersonFile, updatePersonFile} from "@/api/business/personFile";


export default {
  name: "PersonFileForm",
  components: {PersonSelect, TreeSelect},
  emits: ['change'],
  data() {
    return {
      dialogVisible: false,
      directoryTree: [],
      form: {
        id: undefined,
        name: undefined,
        fileType: undefined,
        fileTopType: undefined,
        personId: undefined,
        fileFromTime: undefined,
        coverImage: undefined
      },
      formRules: {
        personId: [
          {required: true, message: "档案所属人不能为空", trigger: "blur"}
        ],
        fileType: [
          {required: true, message: "请选择档案类型", trigger: "blur"}
        ],
        name: [
          {required: true, message: "请输入档案名称", trigger: "blur"}
        ],
      }
    }
  },
  methods: {
    open(data) {
      this.reset()
      Object.assign(this.form, data)
      this.dialogVisible = true
    },
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        fileType: undefined,
        fileTopType: undefined,
        personId: undefined,
        fileFromTime: undefined,
        coverImage: undefined
      }
    },
    typeSelect(node) {
      if (node && node.id) {
        if (node.parentId === 0) {
          this.form.fileTopType = node.id
        } else {
          this.form.fileTopType = node.parentId
        }
      } else {
        this.form.fileTopType = undefined
      }
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            updatePersonFile(this.form).then(res => {
              this.$emit('change')
              this.close()
            })
          } else {
            addPersonFile(this.form).then(res => {
              this.$emit('change')
              this.close()
            })
          }

        }
      })
    },
    close() {
      this.dialogVisible = false
    },

  },
  created() {
    directoryTree().then(res => {
      this.directoryTree = res.data
    })
  }
}

</script>

<style scoped>

</style>